<?php defined('BASEPATH') OR exit('No direct script access allowed');

/**
 * @author  Tran Van Thanh
 * @email   thanhtran@vietnambiz.com
 * @date    06.09.2011
 */
 
class Articles_model extends MY_Model {
    
    function __construct()
    {
        parent::__construct();
        $this->_table = 'articles';
    }
    
    function get_articles($page = 1)
    {
    	$this->db->order_by('time_create DESC');
    	$limit = $this->config->item('articles', 'limit');
    	return $this->limit($limit, ($page - 1)*$limit)->get_all();
    }

 	function get_images($articles_id)
    {
        if(is_numeric($articles_id)) {
        	$a[] = $articles_id;
        	$articles_id = $a;
        }
    	return $this->db->where_in('id_articles', $articles_id)->get('images_news')->result();
    }

    function check_id_products($id_product = null)
    {    	
    	if($id_product != "")
    	{
	    	$q  =   $this->db->select()->from("products")->where_in('id',explode(',',$id_product))->get();    	
	        if($q->row())
	        {        	
	        	return true;
	        }
	        else
	        {
	        	return false;
			}
    	}
    	else
    	return true;
	}  

    function delete_articles($articles_id)
    {
        if(is_numeric($articles_id)) {
        	$a[] = $articles_id;
        	$articles_id = $a;
        }
    	$this->db->where_in('id_product', $articles_id)->delete('images_news');
    	$this->db->where_in('id', $articles_id)->delete('articles');
    	$this->delete_many($articles_id);
    }
    
    function get_id_products_with_articles($articles_id = null)    
    {
    	$this->db->select('id_product')
                 ->from('articles_products')
                 ->where('id_articles',$articles_id);
        $q = $this->db->get();
        $result = $q->result();
        $q->free_result();
        return $result;
    }
    
    /*
     * 
     */
    function get_other_articles($where = array(), $limit = 10)
    {
    	return $this->order_by('time_create','DESC')->limit($limit)->get_many_by($where);
    }
}